<?php
/**
 * 用户登录控制器
 */
namespace app\admin\controller;
use think\Controller;
use app\admin\model\admin;
use think\Request;
use think\Session;
class login extends Controller
{
	public function index(){
		return $this->fetch('admin/login');
	}
	public function usercheck(){
		$captcha = input('verify');
		$username = input('username');
		$password = input('password');
		//json返回数据
		//1为验证码错误
		//2为用户名或者密码错误
		//3为登陆成功
		$request = Request::instance();
		if ($request->isPost() == false) {
			$this->redirect('index');
		}
		if (!captcha_check($captcha)) {
			//验证码错误
			$data = ["result"=>1, "msg"=>"验证码错误"];
			echo json_encode($data);
		}else{
			$request = Request::instance();
			//查询管理员账号信息
			$res = admin::get(1);
			$res->toArray();
			//验证码正确
			//(&8*5.)----密码混淆
			if ($_POST['username'] == $res['username'] and sha1($_POST['password'].'(&8*5.)') == $res['password']) {
				Session::set('username',$res['username']);
				Session::set('password',$res['password']);
				Session::set('nickname',$res['nickname']);
				Session::set('email',$res['email']);
				session::set('ip',$request->host());
				Session::set('url',$request->domain());
				$res->loginip = $request->host();
				$res->logintime = time();
				$res->save();
				$data = ["result"=>3, "msg"=>"登陆成功,正在进入管理中心", "url"=>"../user"];
				echo json_encode($data);
			}else{
				$data = ["result"=>2, "msg"=>"用户名或者密码错误"];
				echo json_encode($data);
			}
		}
		

	}
}
